import com.sun.deploy.util.WinRegistry;

import javax.swing.*;
import java.io.File;

/**
 * @author gcl
 * @date 2022/12/13 14:12
 */
public class SitRegeditFix {

    final static String EFT_MARK = "SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\EscapeFromTarkov";

    public static void main(String[] args) {
        try{
            String UninstallString = getValueByKey("UninstallString");
            if ( UninstallString == null ){
                System.out.println("请先使用[SPT-AKI管理器.exe]的[常用功能>过正版验证]!");
                System.out.println("请键入回车已退出此页面!");
                System.in.read();
                return;
            }
            File EscapeFromTarkovExe = chooseSomeThing(JFileChooser.FILES_ONLY);
            File InstallLocation = chooseSomeThing(JFileChooser.DIRECTORIES_ONLY);
            // 写入数据
            if ( !setValueByKey("DisplayVersion", "0.12.12.31.19428") ){
                System.out.println("客户端版本号写入失败!");
            }
            System.out.println("客户端版本号写入成功!");
            if ( !setValueByKey("EscapeFromTarkov.exe", EscapeFromTarkovExe.getPath() ) ){
                System.out.println("客户端启动器路径写入失败!");
            }
            System.out.println("客户端启动器路径写入成功!");
            if ( !setValueByKey("InstallLocation", InstallLocation.getPath() ) ){
                System.out.println("客户端路径写入失败!");
            }
            System.out.println("客户端路径写入成功!");
            System.out.println("请键入回车已退出此页面!");
            System.in.read();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static String getValueByKey(String key){
        return WinRegistry.getString(WinRegistry.HKEY_LOCAL_MACHINE, EFT_MARK, key);
    }

    public static boolean setValueByKey(String key,String value){
        return WinRegistry.setStringValue(WinRegistry.HKEY_LOCAL_MACHINE, EFT_MARK, key, value);
    }

    /**
     * 用于选择文件夹或文件
     * @param fileChooserType 请参考JFileChooser的参数
     * @return 获取到的文件对象
     */
    private static File chooseSomeThing(int fileChooserType ){
        JFileChooser fileChooser=new JFileChooser();
        if ( fileChooserType == JFileChooser.DIRECTORIES_ONLY ){
            fileChooser.setDialogTitle("请选择客户端文件夹");
        } else if (fileChooserType == JFileChooser.FILES_ONLY) {
            fileChooser.setDialogTitle("请选择客户端文件夹下的EscapeFromTarkov.exe文件");
        }
        fileChooser.setCurrentDirectory(new File("."));
        fileChooser.setFileSelectionMode(fileChooserType);
        int state = fileChooser.showOpenDialog(null);
        if ( state == JFileChooser.APPROVE_OPTION){
            return fileChooser.getSelectedFile();
        }
        throw new RuntimeException("文件获取失败");
    }

}
